<?php



/**
 * Handles everything that has to do with HTTP.
 */
class HTTPHandler {
	private static $getParams;	// Asociative array holding all GET parameters.
	private static $postParams;	// Asociative array holding all POST parameters.
	private static $buffer;		// A buffer containing all output.
	

	
	/**
	 * Gets the URI of the request.
	 * 
	 * @return The URI of the request.
	 */
	public static function getFullURI() {
		return $_SERVER["REQUEST_URI"];
	}
	
	
	
	/**
	 * Gets the operation name. (REST)
	 * 
	 * @return The REST opeartion name.
	 */
	public static function getRESTOperation() {
		// Detach query:
		$uri = "http://www.dummy.com" . HTTPHandler::getFullURI();
		$query = parse_url($uri, PHP_URL_PATH);
		
		// The operation name is after the last slash:
		$operationName = substr($query, strrpos($query, '/')+1);
		return $operationName;
	}
	
	
	/**
	 * Gets a value in the request header.
	 * 
	 * @param headerName	The name for the requested header.
	 * @return The value of the given header or NULL if it does not exist.
	 */
	public static function getHeader($headerName) {
		// Return the header value if it exists:
		$headerName = strtoupper($headerName);
		if(isset($_SERVER["HTTP_" . $headerName])) {
			return $_SERVER["HTTP_" . $headerName];
			
		// Return NULL if it doesn't:
		} else {
			return NULL;
		}
	}

	
	/**
	 * Sets a header variable.
	 * 
	 * @param headerName 	Name of the header.
	 * @param headerValue	Value for the header.
	 */
	public static function setHeader($headerName, $headerValue) {
		header($headerName . ": " . $headerValue);
	}

	/**
	 * Adds the given string the the reply buffer.
	 * 
	 * @param string	String to add to the output buffer.
	 */
	public static function add($string) {
		self::$buffer .= $string;
	}

	/**
	 * Clears the reply buffer.
	 */
	public static function clear() {
		self::$buffer = NULL;
	}
	
	
	/**
	 * Flushes and clears the reply buffer.
	 */
	public static function flush() {
		// Flush:
		echo self::$buffer;
		
		// Clear buffer:
		self::clear();
		
		// Log:
		Logger::log("HTTPHandler", "Outputbuffer flushed.");
	}
	
	
	/**
	 * Gets a POST parameter.
	 * 
	 * @param paramName Name of the POST parameter to fetch.
	 */
	public static function getPostParam($paramName) {
		// Check if POST vars were loaded:
		if (!self::$postParams) {
			self::loadPostParams();
		}
		
		// Return param:
		return self::$postParams[$paramName];
	}
	
	/**
	 * Gets a GET parameter.
	 * 
	 * @param paramName Name of the GET parameter to fetch.
	 */
	public static function getGetParam($paramName) {
		// Check if GET vars were loaded:
		if (!self::$getParams) {
			self::loadGetParams();
		}
		
		// Return param:
		return self::$getParams[$paramName];
	}
	
	/**
	 * Gets the client's IP address.
	 */
	public static function getIP() {
		return $_SERVER["REMOTE_ADDR"];
	}
	
	
	
	
	
	/**
	 * Loads the GET parameters.
	 */
	private static function loadGetParams() {
		// Reset:
		Logger::log("HTTPHandler", "Loading GET parameters.");
		self::$getParams = NULL;
		
		// Detach GET parameters from URI and store them:
		$uri = "http://www.dummy.com" . self::getFullURI();
		$query = parse_url($uri, PHP_URL_QUERY);
		parse_str($query, self::$getParams);
		Logger::log("HTTPHandler", "GET parameters loaded.");
	}
	
	/**
	 * Loads the POST parameters.
	 */
	private static function loadPostParams() {
		// Reset:
		Logger::log("HTTPHandler", "Loading POST parameters.");
		self::$postParams = NULL;
		
		// Simple assignment:
		self::$postParams = $_POST;
		Logger::log("HTTPHandler", "POST parameters loaded.");
	}

}
?>
